package com.yami.shop.platform.task;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.yami.shop.bean.model.*;
import com.yami.shop.bean.param.SendUserDropsParam;
import com.yami.shop.bean.param.UserUpdateParam;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.personnft.common.service.NftTansferEnpandService;
import com.yami.shop.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Component
@AllArgsConstructor
public class HwjTakeGoodsTask {

    @Autowired
    private HwjTakeGoodsService hwjTakeGoodsService;
    @Autowired
    private NftTansferEnpandService nftTansferEnpandService;
    @Autowired
    private ProductService productService;
    @Autowired
    private UserService userService;
    @Autowired
    private ChannelUserRelationRecordService channelUserRelationRecordService;
    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private BusinessNftService businessNftService;

    @XxlJob("hwjTakeGoodsTask")
    public void hwjTakeGoodsTask() {
        // 查询待处理数据
        List<HwjTakeGoods> hwjTakeGoodsList = hwjTakeGoodsService.list(new QueryWrapper<HwjTakeGoods>().eq("status", "0"));

        log.info("海文交提现任务开始----------------------");
        for(HwjTakeGoods hwjTakeGoods : hwjTakeGoodsList){
            // 校验用户信息 --- 是否存在
            User user = userService.getOne(new QueryWrapper<User>().eq("id_number", hwjTakeGoods.getIdNumber()));
            if(ObjectUtil.isEmpty(user)){
                continue;
            }
            // 查询该商品属于那个链
            PersonnftTansferEnpand byVarietyCode = nftTansferEnpandService.getByVarietyCode(hwjTakeGoods.getVarietyCode());

            Product product = productService.getById(byVarietyCode.getProdId());
            // 校验用户信息 --- 是否导入链信息
            Boolean hasAllAddress = channelUserRelationRecordService.isHasAllAddress(user.getUserId(), product.getChain(), product.getChannelCode(), product.getSource() == 0 ? "1" : "2");
            if (!hasAllAddress) {
                continue;
            }
            // businessNft添加数据
            UserUpdateParam param = new UserUpdateParam();
            // 商品信息
            List<SendUserDropsParam> userDropsParams = new ArrayList<>();

            SendUserDropsParam sendUserDropsParam = new SendUserDropsParam();
            sendUserDropsParam.setTargetId(product.getProdId());
            sendUserDropsParam.setType(0);
            sendUserDropsParam.setNums(hwjTakeGoods.getNum().intValue());
            userDropsParams.add(sendUserDropsParam);

            param.setDrops(userDropsParams);
            // User
            param.setUserIds(Collections.singletonList(user.getUserId()));
            // 任务空投逻辑
            param.setBusinessType(2);
            // 业务ID
            param.setAirdropBusinessId(hwjTakeGoods.getId());
            ShopDetail shopDetail = shopDetailService.getShopDetailByShopId(product.getShopId());
            businessNftService.bindDropById(param, shopDetail);



            // 修改数据状态
            hwjTakeGoods.setStatus("1");
            hwjTakeGoodsService.updateById(hwjTakeGoods);

        }
    }

}
